<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <!--<script type="text/javascript" src="./js/xadmin.js"></script>-->
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>-->
    <!--<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>-->
    <!--<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>-->
    <!--[endif]-->
    <script type="text/javascript" src="../../common/js/public.js"></script>
    <script type="text/javascript" src="../../common/js/publicUtils.js"></script>
</head>
<body>
<div class="layui-fluid" id="flowDiv" onload="onLoad()">
    <div class="zTreeBackground left" style="float: left;width:250px">
        <div class="titlediv">
            <p style="text-align:right;background-color:white;line-height: 30px">
                数字字典信息<i class="layui-icon layui-icon-refresh" style="margin-left: 36%" onclick="searchByName()"></i>&ensp;&ensp;
            </p>

        </div>
        <div class="searchdiv" >
            <div class="layui-input-inline" style="width:100%">
                <input type="text" id="search" name="keyword" autocomplete="off" class="layui-input" placeholder="请输入名称"/>
                <i class="layui-icon layui-icon-search" style="position: absolute;top:8px;right: 8px;" onclick="searchByName()"></i>
            </div>
        </div>
        <div class="ztreediv" id="ztreediv" style="overflow:auto">
            <div class="ztreeuldiv">
                <ul id="treeStructure" class="ztree" style="overflow:auto"></ul>
            </div>
        </div>
    </div>
    <div class="zTreeBackground right" style="float: left; padding-left:1%">

        <table class="layui-hide" id="datatable"></table>
        <script type="text/html" id="toolbar">
        </script>
    </div>
</div>
</body>
<SCRIPT type="text/javascript">
    var zTreeObj,form,cols,url,tableObj,table,level=0,treeNodeId=-1,selecNode,count=1;

    layui.use(["form"],function () {
        form = layui.form;
    })
    $(document).ready(function(){
        loadbutton("toolbar");//加载按钮loadbutton(id)，id为button容器id，不传默认为buttongroup
        loadTree();
    });
    window.onload = getContentSize;
    window.onresize = getContentSize;

    /**
     * 页面自适应
     */
    function getContentSize() {
        var wh = document.documentElement.clientHeight;
        var eh = 30;
        ch = (wh - eh) + "px";
        document.getElementById( "flowDiv" ).style.height = ch;
        document.getElementById( "ztreediv" ).style.height = (wh - eh - 50)+"px";
        document.body.style.height = (wh)+"px";
        $(".zTreeBackground.right").css("width",($(document).width()-300)+'px');//自适应表格处理
    }
    /**
     * 左侧树设置
     * @type {{check: {enable: boolean}, data: {simpleData: {enable: boolean, idKey: string, pIdKey: string, isParent: string, rootPid: number}}, callback: {beforeClick: zTreeBeforeClick, onClick: zTreeOnClick}}}
     */
    var setting = {
        edit:{
            enable: false,
        },
        async: {
            enable: true,
            url: publicUrl+"infdictsort/selectInfdictsortByPid",
            headers: {"Authorization":getData("Authorization")},
            dataFilter: formatedata,
            otherParam:{//传入查询参数
                //注意，要传入动态变化的参数必须用return的方式
                "name":function(){return $("#search").val()
                }
            },

            autoParam: ["id"]
        },
        data: {
            simpleData: {
                enable: true,
                idKey:"id",
                pIdKey:"pid",
                rootPid:-1
            },
            key: {
                isParent:"parent",
                icon:"xIcon"
            }
        },
        callback: {
            beforeClick: zTreeBeforeClick,
            onClick: zTreeOnClick,
            onAsyncSuccess: selectAppointNode //异步加载数据完成后的回调函数
        }
    };
    /**
     * @Author bxf
     * @Description 搜索框按钮
     * @Date 11:20 2019/7/17
     * @Param 
     * @return 
     **/
    function searchByName(){
        count = 1;
        loadTree();
    }
    /**
     * @Author bxf
     * @Description 刷新左边树
     * @Date 11:20 2019/7/17
     * @Param
     * @return
     **/
    function refreshRoot(){
        count = 1;
        loadTree();
    }

    /**
     * 点击前验证
     * @param treeId
     * @param treeNode
     * @param clickFlag
     * @returns {boolean}
     */
    function zTreeBeforeClick(treeId, treeNode, clickFlag) {
        //return (treeNode.id !== menuid);
        return true;
    };
    /**
     * 加载左侧树
     */
    function loadTree(){
        zTreeObj = $.fn.zTree.init($("#treeStructure"), setting, null);
    }
    /**
     * 选中第一个节点
     * @param treeobj
     * @param id
     */
    function selectAppointNode(event, treeId, treeNode, msg) {
        if ($("#treeStructure_1_a").length > 0 && count ==1) {
            $("#treeStructure_1_a").click();
            count++;
        }
    }

    /**
     * 数据拦截器
     * @param treeId
     * @param parentNode
     * @param responseData
     */
    function formatedata(treeId,parentNode,responseData) {

        return formatTreeParseData(treeId,parentNode,responseData,function(parentNode,responseData){
            var jsonsdata = new Array();
            if(parentNode && parentNode.level==1){
                for(var i=0;i<responseData.data.length;i++){
                    delete responseData.data[i].parent;
                    jsonsdata.push(responseData.data[i]);
                }
            }else{
                jsonsdata = responseData.data;
            }
            return jsonsdata;
        });

    }

    /**
     * 点击触发
     * @param event
     * @param treeId
     * @param treeNode
     */
    function zTreeOnClick(event, treeId, treeNode) {
        if(treeNode){
            level = treeNode.level;
            treeNodeId = treeNode.id;
            selecNode = treeNode;
        }

        url = publicUrl+'infdictsort/selectListByPid';
        cols = [[
            {
                type:'checkbox',
                width: "160px",
				align: 'center',
            },
            {
                type: 'numbers',
                title: '序号',
                width: "160px",
                align: 'center',
            }, {
                field: 'code',
                title: '编码',
                width: "18%",
                sort: true,
                align: 'center',
            },{
                field: 'name',
                title: '名称',
                sort: true,
                width: "18%",
                align: 'center',
            }, {
                field: 'pname',
                title: '所属分类',
                sort: true,
                width: "18%",
                align: 'center',
                templet:function(r){
                    if(treeNode){
                        return treeNode.name;
                    }else{
                        return "数字字典";
                    }
                }
            },{
                field: 'sort',
                title: '排序',
                width: "10%",
                align: 'center',
            }, {
                field: 'comment',
                title: '备注',
                width: "30%",
                align: 'center',
            }]]
        if(level==2){//加载字典项
            url = publicUrl+'infdictitem/selectListByPid';
            cols = [[
                {
                    type:'checkbox',
                    width: "160px",
					align: 'center',
                },
                {
                    type: 'numbers',
                    title: '序号',
                    width: "160px",
                    align: 'center',
                }, {
                    field: 'code',
                    title: '编码',
                    sort: true,
                    width: "18%",
                    align: 'center',
                },{
                    field: 'name',
                    title: '名称',
                    sort: true,
                    width: "18%",
                    align: 'center',
                }, {
                    field: 'pname',
                    title: '所属分类',
                    sort: true,
                    width: "18%",
                    align: 'center',
                }, {
                    field: 'sort',
                    title: '排序',
                    width: "10%",
                    align: 'center',
                }, {
                    field: 'comment',
                    title: '备注',
                    width: "30%",
                    align: 'center',
            }]]
        }
        var width = $(".left-nav").css("width");
        $(".zTreeBackground.right").css("width",($(document).width()-300)+'px');//自适应表格处理
        layui.use(['form','table'], function(){
            form = layui.form;
            table = layui.table;
            tableObj = table.render({
                elem: '#datatable',
                size: 'sm',
                toolbar: '#toolbar',
                even: true, //开启隔行背景
                height: 'full-50',
                headers: {"Authorization":getData("Authorization")},
                method: 'post',
                url: url, //stationId='+window.parent.window.parentId,//8AE7DECA-8E41-4FAB-8B30-9FD1B0B7FA33,78A9E5D8-20FB-4436-BBE8-A25EAC38D056
                where:{
                    id:treeNode.id
                },
                parseData: function(res){ //res 即为原始返回的数据
                    return formatTableParseData(res);
                },
                request: {
                    pageName: 'pageIndex' //页码的参数名称，默认：page
                    ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                response: {
                    statusCode: 2000,
                },
                cols: cols,
                page: true
            });

        });
    };
    /**
     * @Author bxf
     * @Description 添加菜单页面
     * @Date 9:51 2019/7/18
     * @Param 
     * @return 
     **/
    function addInfdict(callback){
        var iframeWin ;
        var nodes = zTreeObj.getSelectedNodes()[0];
        layer.open({
            type: 2,
            area: ["630px" , "500px"],
            fix: false,
            //不固定
            maxmin: false,
            shade: 0.3,
            title: "新增",
            content: "./addinfdict.html",
            btn: ['应用','确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: function(index, layero) {
                 var resultdata = iframeWin.contentWindow.submit(level);//应用
                 if(resultdata && resultdata.code==2000){
                     refreshSelectNode(zTreeObj,nodes.id);
                     pointNode(zTreeObj,nodes.id);
                 }
                 return false;
                 //layer.closeAll();
            },
            btn2: function(index, layero){
            //按钮【按钮二】的回调
                var resultdata = iframeWin.contentWindow.submit(level);//保存
                if(resultdata && resultdata.code==2000){
                    refreshSelectNode(zTreeObj,nodes.id);
                    pointNode(zTreeObj,nodes.id)
                    layer.closeAll();
                }else{
                    return false;
                }

            //return false 开启该代码可禁止点击该按钮关闭
            },
            btn3: function(index, layero){
                //按钮【按钮三】的回调

                //return false 开启该代码可禁止点击该按钮关闭
            },
            cancel: function(index) {
                return true;
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.voluate(1,nodes);//添加按钮
            }
        });
    }

    /**
     * @Author bxf
     * @Description 添加菜单页面
     * @Date 9:51 2019/7/18
     * @Param
     * @return
     **/
    function editInfdict(){
        var obj = table.checkStatus('datatable');
        if(!obj || obj.data.length!=1){
            layer.alert("请选择一项信息进行修改！");
            return false;
        }
        var data = obj.data[0]; //idTest 即为基础参数 id 对应的值
        data.pname = selecNode.name;
        var nodes = zTreeObj.getSelectedNodes()[0];
        var iframeWin ;
        layer.open({
            type: 2,
            area: ["630px" , "500px"],
            fix: false,
            //不固定
            maxmin: false,
            shade: 0.3,
            title: "编辑",
            content: "./addinfdict.html",
            btn: ['确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: function(index, layero) {
                var resultdata = iframeWin.contentWindow.updateInfdict(level);
                if(resultdata && resultdata.code==2000){
                    refreshSelectNode(zTreeObj,nodes.id);
                    pointNode(zTreeObj,nodes.id);
                    layer.closeAll();
                }else {
                    return false;
                }
            },
            cancel: function(index) {
                return true;
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.voluate(2,data);//添加按钮
            }
        });
    }

    function  deleteInfdict() {
        //获取选中项
        var obj = table.checkStatus('datatable');
        if(!obj || obj.data.length<1){
            layer.alert("请至少选择一项信息！");
            return false;
        }
        var ids = new Array();
        for(var i=0;i<obj.data.length;i++){
            ids.push(obj.data[i].id);
        }
        var nodes = zTreeObj.getSelectedNodes()[0];
        layui.use(['layer'],function(){
            var layer = layui.layer;
            layer.confirm('删除父类子类会级联删除，确定删除？', {
                btn: ['确定', '取消'] //可以无限个按钮
            }, function(){
                var delurl = "infdictsort/deleteInfdictsortByIds";
                if(level==2){//删除字典分类详细项
                    delurl = "infdictitem/deleteInfDictItemByIds";
                }
                publicAjax (null,delurl,{"ids":ids.join(",")},null,null,function(r){
                    if(r.code==2000){
                        laysuccess("删除成功",null);
                        refreshSelectNode(zTreeObj,nodes.id);
                        pointNode(zTreeObj,nodes.id);
                    }else{
                        layerror("删除失败",null);
                    }
                })
            }, function(){
                layer.closeAll();

            });

        })
    }


    /**
     * 打开选中指定节点
     * @param treeobj
     * @param id
     */
    function getAppointNode(treeobj,id){
        if(id){
            var node = treeobj.getNodeByParam("id",id);
            treeobj.selectNode(node,true);//指定选中ID的节点
            //treeobj.expandNode(node, true, false);//指定选中ID节点展开
        }
    }



    /**
     * 刷新指定节点
     */
    function refreshSelectNode(obj,id) {
        var node = obj.getNodeByParam("id",id);
        obj.reAsyncChildNodes(node,"refresh");
    }
    /**
     * 选中某个节点
     */
    function pointNode(obj,id){
        var nodeClick = obj.getNodeByParam("id", id);
        obj.selectNode(nodeClick);//选择点
        obj.setting.callback.onClick(null, id, nodeClick);//调用点击事件
    }





</script>
</html>